//视图添加一条新消息
function addMsg(newMsg) {
    // log(newMsg.getElems());
    var msg_content = null;
    var m_obj = {};
    m_obj.fromAccount = newMsg.getFromAccount();
    m_obj.fromAccountNick = newMsg.getFromAccountNick();
    m_obj.time = newMsg.getTime();
    m_obj.isSend = newMsg.getIsSend();
    var msg_list = newMsg.getElems();
    for (var j = 0; j < msg_list.length; j++) {
        var type = msg_list[j].getType();
        switch (type) {
            case MSG_ELEMENT_TYPE.TEXT:
                msg_content = msg_list[j].getContent().getText();
                //转义，防XSS
                m_obj.content = webim.Tool.formatText2Html(msg_content);

                break;
            case MSG_ELEMENT_TYPE.IMAGE:
                m_obj.content = convertImageMsgToHtml(msg_list[j].getContent());
                break;
        }
        im_controller.get_new_msg(m_obj);
    }
}

//解析图片消息元素
function convertImageMsgToHtml(content, imageName) {
    var smallImage = content.getImage(webim.IMAGE_TYPE.SMALL);//小图
    var bigImage = content.getImage(webim.IMAGE_TYPE.LARGE);//大图
    var oriImage = content.getImage(webim.IMAGE_TYPE.ORIGIN);//原图
    if (!bigImage) {
        bigImage = smallImage;
    }
    if (!oriImage) {
        oriImage = smallImage;
    }
    return "<img name='" + imageName + "' src='" + smallImage.getUrl() + "' style='width: 100%;' id='" + content.getImageId() + "' bigImgUrl='" + bigImage.getUrl() + "' onclick='show_img_big(\"" + oriImage.getUrl() + "\")' />";
}


/*list = list.MsgList;
for (var i = 0; i < list.length; i++){
    webim.setAutoRead(list[i].getSession(), true, true);
    //log();
    /!*var m_obj = {};
    m_obj.content = list[i].getElems()[0].getContent().getText();
    m_obj.fromAccount = list[i].getFromAccount();
    m_obj.fromAccountNick = list[i].getFromAccountNick();
    m_obj.time = list[i].getTime();
    m_obj.isSend = list[i].getIsSend();
    _this.talk_info.push(m_obj);*!/

}*/


function addHistoryMsg(obj,newMsg,type) {
    // log(newMsg);
    var msg_content = null;
    var m_obj = {};
    m_obj.fromAccount = newMsg.getFromAccount();
    m_obj.fromAccountNick = newMsg.getFromAccountNick();
    m_obj.time = newMsg.getTime();
    m_obj.isSend = newMsg.getIsSend();
    var msg_list = newMsg.getElems();
    for (var j = 0; j < msg_list.length; j++) {
        var type = msg_list[j].getType();
        switch (type) {
            case MSG_ELEMENT_TYPE.TEXT:
                msg_content = msg_list[j].getContent().getText();
                //转义，防XSS
                m_obj.content = webim.Tool.formatText2Html(msg_content);

                break;
            case MSG_ELEMENT_TYPE.IMAGE:
                m_obj.content = convertImageMsgToHtml(msg_list[j].getContent());
                break;
        }
    }
    if (type == 'unshift'){
        obj.talk_info.unshift(m_obj)
    }else {
        obj.talk_info.push(m_obj)
    }
}
// 添加历史消息到视图
function addHistoryMsgs(obj,his_msg,order_type) {
    var msg_content = null;
    var msg_list = his_msg.MsgList;
    if (order_type == 1){
        for (var j = 0; j < msg_list.length; j++) {
            webim.setAutoRead(msg_list[j].getSession(), true, true);
            var m_obj = {};
            var tmp_list = msg_list[j].getElems();
            m_obj.fromAccount = msg_list[j].getFromAccount();
            m_obj.fromAccountNick = msg_list[j].getFromAccountNick();
            m_obj.time = msg_list[j].getTime();
            m_obj.isSend = msg_list[j].getIsSend();
            for (var i = 0;i < tmp_list.length; i++){
                var type = tmp_list[i].getType();
                // log(type);
                switch (type) {
                    case MSG_ELEMENT_TYPE.TEXT:
                        msg_content = tmp_list[i].getContent().getText();
                        //转义，防XSS
                        m_obj.content = webim.Tool.formatText2Html(msg_content);

                        break;
                    case MSG_ELEMENT_TYPE.IMAGE:
                        m_obj.content = convertImageMsgToHtml(tmp_list[i].getContent());
                        break;
                }
            }
            obj.talk_info.push(m_obj)
        }
    }else {
        for (var j = msg_list.length - 1; j >= 0; j--) {
            webim.setAutoRead(msg_list[j].getSession(), true, true);
            var m_obj = {};
            var tmp_list = msg_list[j].getElems();
            m_obj.fromAccount = msg_list[j].getFromAccount();
            m_obj.fromAccountNick = msg_list[j].getFromAccountNick();
            m_obj.time = msg_list[j].getTime();
            m_obj.isSend = msg_list[j].getIsSend();
            for (var i = 0;i < tmp_list.length; i++){
                var type = tmp_list[i].getType();
                // log(type);
                switch (type) {
                    case MSG_ELEMENT_TYPE.TEXT:
                        msg_content = tmp_list[i].getContent().getText();
                        //转义，防XSS
                        m_obj.content = webim.Tool.formatText2Html(msg_content);

                        break;
                    case MSG_ELEMENT_TYPE.IMAGE:
                        m_obj.content = convertImageMsgToHtml(tmp_list[i].getContent());
                        break;
                }
            }
            obj.talk_info.unshift(m_obj)
        }
    }

}

//拉取聊天记录
var getLastC2CHistoryMsgs = function (selToID,cbOk, cbError) {

    var lastMsgTime = 0;//第一次拉取好友历史消息时，必须传0
    var msgKey = '';
    var options = {
        'Peer_Account': selToID, //好友帐号
        'MaxCnt': getPrePageC2CHistroyMsgNum, //拉取消息条数
        'LastMsgTime': lastMsgTime, //最近的消息时间，即从这个时间点向前拉取历史消息
        'MsgKey': msgKey
    };
    webim.getC2CHistoryMsgs(
        options,
        function (resp) {
            var complete = resp.Complete;//是否还有历史消息可以拉取，1-表示没有，0-表示有
            var retMsgCount = resp.MsgCount;//返回的消息条数，小于或等于请求的消息条数，小于的时候，说明没有历史消息可拉取了

            if (resp.MsgList.length == 0) {
                webim.Log.error("没有历史消息了:data=" + JSON.stringify(options));
                return;
            }
            getPrePageC2CHistroyMsgInfoMap[selToID] = {//保留服务器返回的最近消息时间和消息Key,用于下次向前拉取历史消息
                'LastMsgTime': resp.LastMsgTime,
                'MsgKey': resp.MsgKey
            };
            if (cbOk)
                cbOk(resp);
        },
        cbError
    );
};


//向上翻页，获取更早的好友历史消息
var getPrePageC2CHistoryMsgs = function (obj,selToID, cbOk, cbError) {
    $(document).scrollTop(30);
    var tempInfo = getPrePageC2CHistroyMsgInfoMap[selToID];//获取下一次拉取的c2c消息时间和消息Key
    var lastMsgTime;
    var msgKey;
    if (tempInfo) {
        lastMsgTime = tempInfo.LastMsgTime;
        msgKey = tempInfo.MsgKey;
    } else {
        alert('获取下一次拉取的c2c消息时间和消息Key为空');
        return;
    }
    var options = {
        'Peer_Account': selToID, //好友帐号
        'MaxCnt': getPrePageC2CHistroyMsgNum, //拉取消息条数
        'LastMsgTime': lastMsgTime, //最近的消息时间，即从这个时间点向前拉取历史消息
        'MsgKey': msgKey
    };
    webim.getC2CHistoryMsgs(
        options,
        function (resp) {
            var complete = resp.Complete;//是否还有历史消息可以拉取，1-表示没有，0-表示有
            if (resp.MsgList.length == 0) {
                webim.Log.warn("没有历史消息了:data=" + JSON.stringify(options));
                $(document).scrollTop(15);
                obj.scroll_lock = false;
                weui.alert('没有更多了');
                return;
            }
            getPrePageC2CHistroyMsgInfoMap[selToID] = {//保留服务器返回的最近消息时间和消息Key,用于下次向前拉取历史消息
                'LastMsgTime': resp.LastMsgTime,
                'MsgKey': resp.MsgKey
            };
            if (cbOk){
                cbOk(resp);
            }else{
                //getHistoryMsgCallback(resp.MsgList,true);
            }
        },
        cbError
    );
};